package com.example.demo.lc;

/**
 * @Description TODO
 * @Author wzh
 * @Date 2021/12/9 11:17 上午
 * @Version 1.0
 */
public class Leetcode390 {
    public static void main(String[] args) {
        System.out.println(lastRemaining(9));
    }

    public static int lastRemaining(int n) {
        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }
        if (n % 2 != 0){
            return lastRemaining(n-1);
        }else{
            return 2*(n/2+1-lastRemaining(n/2));
        }
    }
}
